#include <linux/sched.h>
#include "../inc/sched.h"

void __block_task(struct rq *rq, struct task_struct *p)
{
    p->on_rq = 0;
}

static inline void block_task(struct rq *rq, struct task_struct *p, int flags)
{
	if (dequeue_task(rq, p, DEQUEUE_SLEEP | flags))
		__block_task(rq, p);
}

/*
 * Helper function for __schedule()
 *
 * If a task does not have signals pending, deactivate it
 * Otherwise marks the task's __state as RUNNING
 */
static bool try_to_block_task(struct rq *rq, struct task_struct *p, unsigned long task_state)
{
    int flags = DEQUEUE_NOCLOCK;

    /*
     * __schedule()			ttwu()
     *   prev_state = prev->state;    if (p->on_rq && ...)
     *   if (prev_state)		    goto out;
     *     p->on_rq = 0;		  smp_acquire__after_ctrl_dep();
     *				  p->state = TASK_WAKING
     *
     * Where __schedule() and ttwu() have matching control dependencies.
     *
     * After this, schedule() must not care about p->state any more.
     */
    block_task(rq, p, flags);

    return true;
}
